package com.zzh.takeoutapp.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.base.MyBaseAdapter;
import com.zzh.takeoutapp.entity.FoodBean;
import com.zzh.takeoutapp.entity.SelectShopBean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/8/7.
 */

public class SelectShopAdapter extends BaseQuickAdapter<SelectShopBean, BaseViewHolder> {
    public static final int FIRST_STICKY_VIEW = 1;
    public static final int HAS_STICKY_VIEW = 2;
    public static final int NONE_STICKY_VIEW = 3;
    private List<SelectShopBean> flist;

    public SelectShopAdapter(@Nullable List<SelectShopBean> data) {
        super(R.layout.item_select_shop, data);
        this.flist = data;
    }

    private TextSortOnClickListener textSortOnClickListener;

    public interface TextSortOnClickListener {
        void onSortClick(View v, SelectShopBean item);
    }

    public void setOnSortClick(TextSortOnClickListener mOnItemClickLitener) {
        this.textSortOnClickListener = mOnItemClickLitener;
    }

    @Override
    protected void convert(BaseViewHolder helper, final SelectShopBean item) {
        helper.getView(R.id.stick_header).setVisibility(View.VISIBLE);
        helper.setText(R.id.city, item.getKey());
        if (item.getType().equals("附近地址")) {
            helper.getView(R.id.ll_address).setVisibility(View.GONE);
        } else {
            helper.getView(R.id.ll_address).setVisibility(View.VISIBLE);
            helper.setText(R.id.tv_user_name, item.getName());
            helper.setText(R.id.tv_phone, item.getPhone());
        }
        helper.getView(R.id.shop_addr).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textSortOnClickListener.onSortClick(v, item);
            }
        });


        if (helper.getAdapterPosition() == 0) {
            helper.setVisible(R.id.stick_header, true)
                    .setText(R.id.tv_header, item.getType())
                    .setTag(R.id.food_main, FIRST_STICKY_VIEW);
        } else {
            if (!TextUtils.equals(item.getType(), flist.get(helper.getAdapterPosition() - 1).getType())) {
                helper.setVisible(R.id.stick_header, true)
                        .setText(R.id.tv_header, item.getType())
                        .setTag(R.id.food_main, HAS_STICKY_VIEW);
            } else {
                helper.setVisible(R.id.stick_header, false)
                        .setTag(R.id.food_main, NONE_STICKY_VIEW);
            }
        }
        helper.getConvertView().setContentDescription(item.getType());
    }
}
